package org.czx.datactruct.second;

import org.czx.datactruct.first.BagInterface;

/**
 * @author czx
 * @date 2020/8/20 16:07
 */
public class ArrayBagDemo1 {
    public static void main(String[] args) {
        BagInterface<String> aBag = new ArrayBag<>();
        String[] contentOfBag1 = {"A", "A", "B", "A", "C", "A"};
        testAdd(aBag, contentOfBag1);

        aBag = new ArrayBag<>(7);
        String[] contentOfBag2 = {"a", "b", "a", "c", "b", "a", "d"};
        testAdd(aBag, contentOfBag2);
    }

    /**
     * 测试add方法
     * @param aBag
     * @param content
     */
    private static void testAdd(BagInterface<String> aBag, String[] content) {
        System.out.println("Adding following " + content.length + " strings to the bag:");
        for (int i = 0; i < content.length; i++) {
            if (aBag.add(content[i])) {
                System.out.print(content[i] + " ");
            } else {
                System.out.print("\nUnable to add " + content[i] + " to the bag");
            }
        }
        System.out.println();
        displayBag(aBag);
    }

    private static void displayBag(BagInterface<String> aBag) {
        System.out.println("The bag contains following strings: ");
        Object[] bagArray = aBag.toArray();
        for (int i = 0; i < bagArray.length; i++) {
            System.out.print(bagArray[i] + " ");
        }
        System.out.println();
    }
}
